﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ABCLogistic_System_V2._0.BL;
using ABCLogistic_System_V2._0.DA;

namespace ABCLogistic_System_V2._0.GUI
{
    public partial class frmQTHT : Form
    {
        ABCLogisticsEntities context = new ABCLogisticsEntities();
        NHANVIEN nhanvien = new NHANVIEN();
        public bool bEdit = false;
        bool bTimkiem=true;

        public frmQTHT()
        {
            InitializeComponent();
        }
        #region Xu ly khi nhan vao cac button
        // khai bao cac form
        frmMainInterface giaodienchinh;
        frmQTHT_Nguoidung nguoidung;
        frmQTHT_Phanquyen phanquyen;
        frmQTHT_Quydinhhethong quydinhhethong;

        /// <summary>
        /// xu ly quay ve giao dien chinh khi nhan nut giao dien chinh
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGiaodienchinh_Click(object sender, EventArgs e)
        {
            giaodienchinh = new frmMainInterface();
            giaodienchinh.Show();
            this.Close();
        }

        /// <summary>
        /// hien giao dien them nguoi dung khi nhan them nguoi dung
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnThemnguoidung_Click(object sender, EventArgs e)
        {
            nguoidung = new frmQTHT_Nguoidung();
            nguoidung.Text = "Thêm người dùng";
            nguoidung.lblchange.Text = "Thêm người dùng";
            nguoidung.ok += new frmQTHT_Nguoidung.LoadNhanvien(nguoidung_ok);
            nguoidung.ShowDialog();
            
        }

        void nguoidung_ok(bool checkok)
        {
            if (checkok == true)
            {
                LoadList("HOVATEN", "MANV");
            }
        }

        /// <summary>
        /// hien form phan quyen khi nhan vao phan quyen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            phanquyen = new frmQTHT_Phanquyen();
            phanquyen.ShowDialog();
        }

        /// <summary>
        /// hien form quy dinh he thong khi nhan vao quy dinh he thong
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuydinh_Click(object sender, EventArgs e)
        {
            quydinhhethong = new frmQTHT_Quydinhhethong();
            quydinhhethong.ShowDialog();
        }
        #endregion

        private void LoadList(string DisplayMember, string ValueMember)
        {
            var timnhanvien = from p in context.NHANVIENs
                              select new {p.MANV,p.SOCMND,p.HOVATENLOT,p.TEN,p.TENKHAC,p.GIOITINH,p.DIACHI,p.NGAYSINH,p.SODIENTHOAI,p.FAX,p.EMAIL,p.NHOMNGUOIDUNG.TENNHOM,p.NGAYVAOCONGTY };
            grdthongtin.DataSource = timnhanvien.ToList();
            var timnhanvien1 = from cat in context.NHANVIENs
                               select cat;
            cbxTimkiem.DataSource = timnhanvien1.ToList();
            cbxTimkiem.DisplayMember = DisplayMember;
            cbxTimkiem.ValueMember = ValueMember;
            grdthongtin.DataSource = timnhanvien1.ToList();
            cbxTimkiem.Text = "";
        }

        public void frmQTHT_Load(object sender, EventArgs e)
        {
            bTimkiem = true;
            LoadList("HOVATEN", "MANV");

        }

        private void cbxTimkiem_TextChanged(object sender, EventArgs e)
        {
            string strtimkiem = cbxTimkiem.Text;
            if (bTimkiem == true)
            {
                var tennhanvien = from p in context.NHANVIENs
                               where p.HOVATEN.Contains(strtimkiem)
                               select p;
                grdthongtin.DataSource = tennhanvien.ToList();
                bTimkiem = false;
            }
            else
            {
                if (bTimkiem == false)
                {
                    var manhanvien = from p in context.NHANVIENs
                                     where p.MANV.Contains(strtimkiem)
                                     select p;
                    grdthongtin.DataSource = manhanvien.ToList();
                    bTimkiem = true;
                }
            }
        }

        /// <summary>
        /// xu ly khi nhap vao dong tren datagridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grdthongtin_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bEdit = true;
            string strmanv = grdthongtin.CurrentRow.Cells[0].Value.ToString();
            frmQTHT_Nguoidung nguoidung1 = new frmQTHT_Nguoidung(strmanv, bEdit);
            nguoidung1.lblchange.Text = "Chỉnh sửa thông tin người dùng";
            nguoidung1.Text = "Chỉnh sửa thông tin người dùng";
            nguoidung1.ShowDialog();
        }

        private void radmanhanvien_CheckedChanged(object sender, EventArgs e)
        {
            LoadList("MANV", "MANV");
            bTimkiem = false;
        }

        private void radtennhenvien_CheckedChanged(object sender, EventArgs e)
        {
            LoadList("HOVATEN", "MANV");
            bTimkiem = true;
        }
    }
}
